/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupE.impl;

import edu.upf.grupE.Controller.PictureController;
import edu.upf.grupE.Entities.Picture;
import edu.upf.grupE.Listener;
import edu.upf.grupE.interftest.ImageSelection;
import java.util.Vector;
import javax.swing.ImageIcon;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;

/**
 *
 * @author xday666
 */
public class SearchProfileListener implements Listener {
    /*this class implements the listener which will be prepared to understand the searchProfile
    Packets.*/

    private IM im;

    /**
     *
     * @param instancem
     */
    public SearchProfileListener(final IM instancem) {
        im = instancem;
    }

    /**
     * we only work with the packets that are Messages. then we create a new Message
     * that will be the response to the search profile Message.
     * To be sure that the information that arrives to this listener is correct, we
     * print it into the Text Box.
     * Later we send back to the origin a Message containing the same object received.
     * @param packet
     */
    public final void processPacket(final Packet packet) {
        if (packet instanceof Message) {



            Message msg = (Message) packet;


            searchProfile searchPic = (searchProfile) msg.getProperty("searchPic");
            Vector<Picture> result = new Vector<Picture>();

            System.out.println("Recibido msg de buskeda de " + msg.getFrom());

            switch (searchPic.type) {
                case searchProfile.BYNEWSADJACENT:
                    result = PictureController.searchByNewsAdjacent(searchPic.key);
                    break;
                case searchProfile.BYNEWSTEXT:
                    result = PictureController.searchByNewsText(searchPic.key);
                    break;
                case searchProfile.BYNEWSTITLE:
                    result = PictureController.searchByNewsTitle(searchPic.key);
                    break;
                case searchProfile.BYONTOLOGY:
                    result = PictureController.searchByOntology(searchPic.key, searchPic.ontology);
                    break;
                case searchProfile.FREETEXT:
                    result = PictureController.searchFreeText(searchPic.key);
                    break;
            }
            if(result.size()<1) System.out.println("No se allaron resultados");
            for (Picture tmp : result) {
                System.out.println("Recibida peticion de: " + msg.getFrom() + " enviando " + result.size() +" resultados.");
                Message searchResult = new Message();
                searchResult.setTo(msg.getFrom());

                Picture temp = (Picture) tmp;
                String path = temp.getFileDescriptor().getPath();


                searchResult.setProperty("searchResultPic", temp);
                searchResult.setPacketID(Config.PACKETSEARCHRESULTID);
                im.send(searchResult);
            }


        }
    }
}
